﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kortex.WallMgr;
using Kortex.Container;
using Kortex.Common;
using Kortex.Communication_objs;
namespace Kortex.WallManagerWallControl
{
    public partial class WallControlGui : UserControl
    {
        int MinBrighNess = 0;
        int MaxBrigtNess = 12000;
        public WallControlGui()
        {
            InitializeComponent();
        }

        public WallControlGui(WallManager obj)
        {
            InitializeComponent();
            mProducer = obj;

        }


        public WallManager mProducer = null;

        private void Handle_responce(Cube_command_reqObj req, General_responce res)
        {
            switch (req.cmdType)
            {
                case commandType.cmd_cs_max_min_brightness_get:

                    if (res.result.min_brightness >= MinBrighNess)
                    {
                        req.guiVal.TextBoxs[0].Text = res.result.min_brightness.ToString();
                        MinBrighNess = res.result.min_brightness;

                    }
                    if (res.result.max_brightness <= MaxBrigtNess)
                    {
                        req.guiVal.TextBoxs[1].Text = res.result.max_brightness.ToString();
                        MaxBrigtNess = res.result.max_brightness;
                    }
                    break;

            }
        }

        public void WallControllerResponce(Cube_command_reqObj req_obj, General_responce res)
        {
            if (req_obj != null)
            {
                if (req_obj.ReturnBox != null)
                {
                    if (res.error != null)
                    {
                        req_obj.ReturnBox.Text = res.error.message;
                        req_obj.ReturnBox.BackColor = Color.Red;

                    }
                    else
                    {
                        req_obj.ReturnBox.Text = res.result.@return;
                        req_obj.ReturnBox.BackColor = Color.LightGreen;

                        Handle_responce(req_obj, res);
                    }
                }


            }


        }

        #region IMmage tab Commands


        private void Wal_Img_MercurySetSource_butt_Click(object sender, EventArgs e)
        {
            GuiBoxArray gui = new GuiBoxArray();
            gui.Add(Image_MercurySourceList_combo);
            Wall_SetInputSelecte_result.BackColor = Color.Pink;

            string[] parm = new string[1];
            parm[0] = Image_MercurySourceList_combo.Text;

            Cube_command_reqObj req = new Cube_command_reqObj(cmd_requster.wallControler, 1, 0, 0, commandType.cmd_set_input_selected, parm, Wall_SetInputSelecte_result, gui);

            mProducer.WallControlRequest(req, "Set Source");
        }

        private void Wal_Img_MercurySetSourceClear_butt_Click(object sender, EventArgs e)
        {

        }
        private void Addedid_options_to_combo(string input_p)
        {


            switch (input_p)
            {
                case "dvi1":
                case "dvi2":
                    this.img_DDC_timing_combo.Items.AddRange(new object[] {
            "ddc_1920_1080_25000",
            "ddc_1920_1080_25500",
            "ddc_1920_1080_29970",
            "ddc_1920_1080_30500",
            "ddc_1920_1080_50000",
            "ddc_1920_1080_50500",
            "ddc_1920_1080_59940",
            "ddc_1920_1080_60500",
            "ddc_1920_2160_25000",
            "ddc_1920_2160_25500",
            "ddc_1920_2160_29970",
            "ddc_1920_2160_30500",
            "ddc_3840_1080_25000",
            "ddc_3840_1080_25500",
            "ddc_3840_1080_29970",
            "ddc_3840_1080_30500",
            "ddc_custom"});
                    img_DDC_timing_combo.Text = "ddc_1920_1080_25000";
                    break;
                case "hdmi":
                case "dp":
                case "ops":
                    this.img_DDC_timing_combo.Items.AddRange(new object[] {
            "ddc_1920_1080_25000",
            "ddc_1920_1080_25500",
            "ddc_1920_1080_29970",
            "ddc_1920_1080_30500",
            "ddc_1920_1080_50000",
            "ddc_1920_1080_50500",
            "ddc_1920_1080_59940",
            "ddc_1920_1080_60500",
            "ddc_1920_2160_25000",
            "ddc_1920_2160_25500",
            "ddc_1920_2160_29970",
            "ddc_1920_2160_30500",
            "ddc_1920_2160_50000",
            "ddc_1920_2160_50500",
            "ddc_1920_2160_59940",
            "ddc_1920_2160_60500",
            "ddc_1920_3240_25000",
            "ddc_1920_3240_25500",
            "ddc_1920_3240_29970",
            "ddc_1920_3240_30500",
            "ddc_3840_1080_25000",
            "ddc_3840_1080_25500",
            "ddc_3840_1080_29970",
            "ddc_3840_1080_30500",
            "ddc_3840_1080_50000",
            "ddc_3840_1080_50500",
            "ddc_3840_1080_59940",
            "ddc_3840_1080_60500",
            "ddc_3840_2160_25000",
            "ddc_3840_2160_25500",
            "ddc_3840_2160_29970",
            "ddc_3840_2160_30500",
                    "ddc_custom"});
                    img_DDC_timing_combo.Text = "ddc_1920_1080_25000";
                    break;
                default:
                    this.img_DDC_timing_combo.Items.AddRange(new object[] {
            "ddc_1920_1080_25000",
            "ddc_1920_1080_25500",
            "ddc_1920_1080_29970",
            "ddc_1920_1080_30500",
            "ddc_1920_1080_50000",
            "ddc_1920_1080_50500",
            "ddc_1920_1080_59940",
            "ddc_1920_1080_60500",
            "ddc_1920_2160_25000",
            "ddc_1920_2160_25500",
            "ddc_1920_2160_29970",
            "ddc_1920_2160_30500",
            "ddc_3840_1080_25000",
            "ddc_3840_1080_25500",
            "ddc_3840_1080_29970",
            "ddc_3840_1080_30500",
             "ddc_custom"});
                    img_DDC_timing_combo.Text = "ddc_1920_1080_25000";
                    break;
            }
        }


        private void Image_Set_edid_input_combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < img_DDC_timing_combo.Items.Count; i++)
                img_DDC_timing_combo.Items.RemoveAt(i);

            img_DDC_timing_combo.Items.Clear();
            Addedid_options_to_combo(Image_Set_edid_input_combo.Text);
        }

        private void img_DDC_timing_combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (img_DDC_timing_combo.Text == "ddc_custom")
            {
                Image_Set_edid_filePath_show.Enabled = true;
                Img_edid_Browse_butt.Enabled = true;
            }
            else
            {
                Image_Set_edid_filePath_show.Enabled = false;
                Img_edid_Browse_butt.Enabled = false;
            }
        }
        private void SetEdid(string Filepath)
        {

            GuiBoxArray gui = new GuiBoxArray();
            gui.Add(Image_MercurySourceList_combo);
            Image_Set_edid_result_show.BackColor = Color.Pink;

            string[] parm = new string[3];
            parm[0] = Image_Set_edid_input_combo.Text;
            parm[1] = Filepath;
            parm[2] = Image_Set_edid_filePath_show.Text; //Only For Custom


            Cube_command_reqObj req = new Cube_command_reqObj(cmd_requster.cubeControler, 0, 0, commandType.cmd_edid_set, parm, Image_Set_edid_result_show, gui);

            mProducer.WallControlRequest(req, "Set Edid");
        }

        private void VerifyEdit(string Filepath)
        {

            GuiBoxArray gui = new GuiBoxArray();
            gui.Add(Image_MercurySourceList_combo);
            Image_Set_edid_result_show.BackColor = Color.Pink;

            string[] parm = new string[3];
            parm[0] = Image_Set_edid_input_combo.Text;
            parm[1] = Filepath;
            parm[2] = Image_Set_edid_filePath_show.Text; //Only For Custom


            Cube_command_reqObj req = new Cube_command_reqObj(cmd_requster.cubeControler, 0, 0, commandType.cmd_edid_verify, parm, Image_Set_edid_result_show, gui);

            mProducer.WallControlRequest(req, "Verify Edid");

        }

        private void Image_Set_edid_butt_set_Click(object sender, EventArgs e)
        {
            string filePath = string.Empty;
            string Intput = Image_Set_edid_input_combo.Text + "/";

            if (Intput == "ops/") Intput = "hdmi/";
            if (Intput == "dvi1/" || Intput == "dvi2/") Intput = "dvi/";

            filePath = "/opt/fmw/edid_files/OVD_KVD_XX21/" + Intput + img_DDC_timing_combo.Text + ".bin";

            if (Image_Set_edid_Set_Radio_butt.Checked == true)
            {
                if (img_DDC_timing_combo.Text == "ddc_custom")
                    SetEdid(string.Empty);
                else
                    SetEdid(filePath);

            }

            if (Image_Set_edid_Verify_Radio_butt.Checked == true)
            {
                if (img_DDC_timing_combo.Text == "ddc_custom")
                    VerifyEdit(string.Empty);
                else
                    VerifyEdit(filePath);


            }
        }

        private void Image_Set_edid_butt_Clearall_Click(object sender, EventArgs e)
        {

        }
        private void Img_edid_Browse_butt_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            if (openFileDialog1.CheckFileExists)
            {
                Image_Set_edid_filePath_show.Text = openFileDialog1.FileName;

            }
            else
                MessageBox.Show(openFileDialog1.FileName + " File Does not exists");
        }

        #endregion

        private void CS_MinMax_brightness_get_Click(object sender, EventArgs e)
        {
            textBox173.BackColor = Color.Pink;
             MinBrighNess = 0;
             MaxBrigtNess = 12000;
            GuiBoxArray gui = new GuiBoxArray(); ;
            gui.Add(textBox172, textBox169);

            string[] parm = null;
            Cube_command_reqObj req = new Cube_command_reqObj(cmd_requster.wallControler, 0, 0, commandType.cmd_cs_max_min_brightness_get, parm, textBox173, gui);
            mProducer.WallControlRequest(req, "max_min_brightness");
        }

        private void CS_MinMax_brightness_get_clearAll_Click(object sender, EventArgs e)
        {

        }

        
    }
}
